package cn.wit.rank.service.impl;

import cn.wit.rank.mapper.CommentAreaMapper;
import cn.wit.rank.mapper.StudyDataMapper;
import cn.wit.rank.mapper.StudentMapper;
import cn.wit.rank.pojo.*;
import cn.wit.rank.service.RankService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public  class RankServiceImpl implements RankService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudyDataMapper studyDataMapper;
    @Autowired
    private CommentAreaMapper commentAreaMapper;

    @Override
    //初始化学习成绩排行榜
    public Result initStudyRecordRank() {
        //业务需要把每个学生的所有成绩全部加起来，然后排序输出
        int count = 0;
        List<RankItem> list = new ArrayList<>();
        List<StudyData> studyDataList = new ArrayList<>();

        List<Student> studentList = new ArrayList<>();
        studentList = studentMapper.selectList(null);
        for (Student i : studentList) {
            QueryWrapper<StudyData> studyDataQueryWrapper = new QueryWrapper<>();
            studyDataQueryWrapper.eq("student_ip", i.getStudentId());
            studyDataList = studyDataMapper.selectList(studyDataQueryWrapper);
            for (int j = 0; j < studyDataList.size(); ++j) {
                count += studyDataList.get(j).getTestScores();

            }
            list.add(new RankItem(i.getStudentImg(), 0, i.getStudentName(), i.getStudentId(), count));
            count = 0;
        }
        List<RankItem> ans = new ArrayList<>();
        for (int j = list.size(); j > list.size() - 4 || j >= 0; --j) {
            list.get(j).setRating(list.size() - j + 1);
            ans.add(list.get(j));

        }


        return Result.ok(ans);
    }

    //初始化完成度排行榜
    @Override
    public Result initStudyCompletenessRank(String className) {
        //将所有课程名==classname的求出来，然后按照学习时间排序
        QueryWrapper<StudyData> studyDataQueryWrapper = new QueryWrapper<>();
        List<StudyData> studyDataList = new ArrayList<>();
        studyDataQueryWrapper.eq("courseName", className).orderByDesc("studyTime");
        studyDataList = studyDataMapper.selectList(studyDataQueryWrapper);
        List<CompletenessRankItem> completenessRankItemList = new ArrayList<>();
        List<Student> studentList = new ArrayList<>();
        Student student = new Student();
        StudyData studyData = new StudyData();
        for (int i = 0; i < studyDataList.size() && i < 5; ++i) {
            studentList.add(studentMapper.selectById(studyDataList.get(i).getStudentId()));
            student = studentList.get(i);
            studyData = studyDataList.get(i);
            completenessRankItemList.add(new CompletenessRankItem(student.getStudentId(), student.getStudentName(), student.getStudentImg(), i + 1, studyData.getStudyTime()));


        }


        return Result.ok(completenessRankItemList);

    }

    //删除评论
    @Override
    public Result deleteCommentAreaItem(int id) {
        int i = commentAreaMapper.deleteById(id);
        if (i == 0)
            return Result.error("删除失败");
        return Result.ok();
    }

    @Override
    public Result initinitStudyCommentArea(String kind) {
        QueryWrapper<CommentAreaItem> commentAreaItemQueryWrapper = new QueryWrapper<>();
        commentAreaItemQueryWrapper.eq("kindKey", kind).ne("parent_id", -1).orderByAsc("number");

        List<CommentAreaItem> lists = commentAreaMapper.selectList(commentAreaItemQueryWrapper);
        return Result.ok(lists);
    }//初始化所有非回复的评论

    @Override
    public Result initReplyCompletenessCommentArea(long id) {
        QueryWrapper<CommentAreaItem>commentAreaItemQueryWrapper=new QueryWrapper<>();
        commentAreaItemQueryWrapper.eq("parent_id",id).orderByAsc("createTime");
        List<CommentAreaItem>commentAreaItems=commentAreaMapper.selectList(commentAreaItemQueryWrapper);




        return Result.ok(commentAreaItems);


    }//根据ID初始化对应评论的回复评论

    @Override
    public Result likeCompletenessComment(long id) {
        int i=0;
        CommentAreaItem  commentAreaItem=commentAreaMapper.selectById(id);
        commentAreaItem.setNumberOfLikes(commentAreaItem.getNumberOfLikes()+1);
        i=commentAreaMapper.updateById(commentAreaItem);
        if(i==0)
            return Result.error("点赞失败");
        return Result.ok();
    }

    //创建评论
    @Override
    public Result createAttention(int userId, String content, String kind) {
        Student student= studentMapper.selectById(userId);
       Date date=new Date();
        CommentAreaItem commentAreaItem=new CommentAreaItem();
        commentAreaItem.setStudentImg(student.getStudentImg());
        commentAreaItem.setStudentName(student.getStudentName());
        commentAreaItem.setCommentContent(content);
        commentAreaItem.setMakerId(userId);
        commentAreaItem.setParentId(-1);
        commentAreaItem.setNumberOfLikes(0);
        commentAreaItem.setCreateTime(date);
        commentAreaItem.setStatus(1);
        commentAreaItem.setKindKey(kind);
       int b= commentAreaMapper.insert(commentAreaItem);
       if(b==0)
           return Result.error("创建评论失败！！！");


        return Result.ok();

    }

    @Override
    public Result replyAttention(int userId, int repliedCommentId, String content, String kind) {
        Student student= studentMapper.selectById(userId);
        Date date=new Date();
        CommentAreaItem commentAreaItem=new CommentAreaItem();
        commentAreaItem.setStudentImg(student.getStudentImg());
        commentAreaItem.setStudentName(student.getStudentName());
        commentAreaItem.setCommentContent(content);
        commentAreaItem.setMakerId(userId);
        commentAreaItem.setParentId(repliedCommentId);
        commentAreaItem.setNumberOfLikes(0);
        commentAreaItem.setCreateTime(date);
        commentAreaItem.setStatus(1);
        commentAreaItem.setKindKey(kind);
        int b= commentAreaMapper.insert(commentAreaItem);
        if(b==0)
            return Result.error("回复评论失败！！！");
        return Result.ok();
    }


}
